var strGetQuery =document.location.search;
var departmentid = cw.common.getQueryString(strGetQuery,'id');
$(document).ready(function(){
	cw.common.setStyle();
	$('#subImg').bind('click',subButton);
	validate();
	cw.common.getUserSession(getDepartmentInfo,true);
})


function getDepartmentInfo(){
	var url = cw.common.path + '/json?action=GET_T_DEPARTMENT_ACTION&DEPARTMENT_NO='+departmentid;
	$.post(url,{},function(data){
		if(data.success){
			var vobj=data.root.T_DEPARTMENT;
			if(vobj==null||vobj==''||vobj==undefined){
				alert("该部门不存在或已被删除");
				window.history.back();
				return;
			}
			else{
				var obj = data.root.T_DEPARTMENT.rs[0];
				$('#DEPARTMENT_NO').attr('value',departmentid);
				$('#DEPARTMENT_NAME').attr('value',obj.DEPARTMENT_NAME);
			}
		}
		else{
			alert(data.errors.errmsg);
		}
	},'json');
	
}

function subFrm(){
	var options = {
	    dataType: 'json',
    	iframe:	false,
	    success: function(data) {
	    	if(data.success){
	    		alert('修改部门信息成功');
	      		window.location.href="department.html";
	      	}else{
	      		alert(data.errors.errmsg);
	      	}
	   	}
	};
	$.ajaxSetup({contentType:'application/x-www-form-urlencoded;charset=utf-8'});
    $('#frm').ajaxSubmit(options);
}

function subButton(){
	return jQuery.formValidator.pageIsValid(1);
}

function validate(){
	$.formValidator.initConfig({formid:"frm",onerror:function(msg){alert(msg)},onsuccess:function(){subFrm();return false;}});
	$("#DEPARTMENT_NAME").formValidator({onshow:"请输入部门名称",oncorrect:"该部门名称可以使用",onfocus:"请输入部门名称"}).inputValidator({min:1,onerror:"部门名称必填"}).inputValidator({max:20,onerror:"真实姓名中文不超过10个字，英文不超过20个字符"})
	.functionValidator({
		fun:function (str,elem){
			var a = $.trim(str);
			if(a.length == 0) return false;
			return true;
		}, onerror: "部门名称必填"
	})
	.ajaxValidator({
		url : cw.common.path + "/json?action=CHECK_DEPARTMENT_BY_UPDATE_ACTION",
		data : "DEPARTMENT_NO=" + departmentid,
		isvalid : true,
		type : 'get',
		datatype : "json",
		success : function(data){
			if(data.success){
	            if(data.root.T_DEPARTMENT != undefined  && data.root.T_DEPARTMENT.rs[0] != null && data.root.T_DEPARTMENT.rs[0] != "" && data.root.T_DEPARTMENT.rs[0] != undefined){
	            	return false;
	            }
	            else{
	            	return true;
	            }
			}else{
				alert('出错了：'+data.errors.errmsg);
				return false;
			}
		},
		buttons: $("#subImg"),
		error: function(){alert("服务器没有返回数据，可能服务器忙，请重试");},
		onerror : "该部门名称已被占用，请更换部门名称",
		onwait : "正在对部门名称进行校验，请稍候..."
	});
}